<?php
class Home_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function getHomeIntrend()
	{
		$dataResult = array();
		//Load Library
		$this->load->library('solr');		
		$dataHomeIntrend = json_decode($this->db->get_where('module_page',array('page' => 'home_intrend'),1)->row()->json);
		if(!empty($dataHomeIntrend)) foreach($dataHomeIntrend as $key => $value):	
			$module_name = strtolower($value->module);
			$searchSolr = 'id:'.$module_name.'_'.$value->value.'';
			switch($module_name):
				case 'song':
				case 'video':
					$dataModuleResult = $this->solr->query($searchSolr)->row();
					$dataAlbumResult = '';
					$dataArtistResult = '';
					if(!empty($dataModuleResult->album_id_i_arr) && empty($dataModuleResult->song_id_i_arr)):
						if(is_array($dataModuleResult->album_id_i_arr)):
							$searchSolr = 'id:album_'.$dataModuleResult->album_id_i_arr{0}.'';
						else:
							$searchSolr = 'id:album_'.$dataModuleResult->album_id_i_arr.'';
						endif;
						$dataAlbumResult = $this->solr->query($searchSolr)->row();
					endif;
					if(!empty($dataModuleResult->artist_id_i_arr)):
						if(is_array($dataModuleResult->artist_id_i_arr)):
							$artist_id_arr = array();
							foreach($dataModuleResult->artist_id_i_arr as $artist_id):
								$artist_id_arr[] = 'id:artist_'.$artist_id.'';
							endforeach;
							$searchSolr = implode(' or ',$artist_id_arr);
						else:
							$searchSolr = 'id:artist_'.$dataModuleResult->artist_id_i_arr.'';
						endif;
						$dataArtistResult = $this->solr->query($searchSolr)->result();
						$artist_name_arr = array();
						foreach($dataArtistResult as $artist):
							$artist_name_arr[] = $artist->artist_name_s;
						endforeach;
					endif;
					$is_mv = 0;
					if($module_name == "video"):
						$is_mv = $this->db->from('video')
						->where("video_id = ".$value->value." AND video_type_id = 1")
						->count_all_results();
					endif;				
					$dataResult[] = array(
						'module' => $module_name,
						'url' => $this->data[''.$module_name.'_url'].(in_array($module_name,array('song','video')) ? 'play/':'archives/').preg_replace('/'.$module_name.'_/','',$dataModuleResult->id).'-'.seo_url($dataModuleResult->{$module_name.'_name_s'}),
						'image' => (!empty($dataAlbumResult) ? $dataAlbumResult->album_image_name_s:$dataModuleResult->{$module_name.'_image_name_s'}),
						'name' => $dataModuleResult->{$module_name.'_name_s'},
						'artist' => @implode(',',$artist_name_arr),
						'is_mv' => $is_mv
					);
				break;
				case 'playlist':
					$dataPlaylist = $this->db->select('playlist_alias,playlist_name,playlist_file_name,playlist_file_ext')
					->get_where('playlist_info',array('playlist_id' => $value->value),1)
					->row();
					$dataResult[] = array(
						'module' => $module_name,
						'url' => $this->data['playlist_url'].'you2play/'.$dataPlaylist->playlist_alias,
						'image' => $dataPlaylist->playlist_file_name.'.'.$dataPlaylist->playlist_file_ext,
						'name' => $dataPlaylist->playlist_name
					);						
				break;
				case 'gallery':
				case 'fashion':
					$dataModule = $this->db->select(''.$module_name.'_id,'.$module_name.'_name')
					->get_where(''.$module_name.'',array(''.$module_name.'_id' => $value->value),1)
					->row();
					$dataModule->image = $this->db->select(''.$module_name.'_file_name,'.$module_name.'_file_ext')
					->order_by("".$module_name."_image_order ASC,".$module_name."_image_id ASC")
					->get_where(''.$module_name.'_image',array(''.$module_name.'_id' => $dataModule->{''.$module_name.'_id'}),1)
					->row();
					$dataResult[] = array(
						'module' => $module_name,
						'url' => $this->data[''.$module_name.'_url'].'archives/'.$dataModule->{''.$module_name.'_id'}.'-'.seo_url($dataModule->{''.$module_name.'_name'}),
						'image' => $dataModule->image->{''.$module_name.'_file_name'}.'.'.$dataModule->image->{''.$module_name.'_file_ext'},
						'name' => $dataModule->{''.$module_name.'_name'}
					);					
				break;
				case 'calendar':
					$dataCalendar = $this->db->select('calendar_id,calendar_subject')
					->get_where('calendar_info',array('calendar_id' => $value->value),1)
					->row();
					$dataCalendar->image = $this->db->select('calendar_file_name,calendar_file_ext')
					->order_by("calendar_image_order ASC,calendar_image_id ASC")
					->get_where('calendar_image',array('calendar_id' => $dataCalendar->calendar_id),1)
					->row();
					$dataResult[] = array(
						'module' => $module_name,
						'url' => $this->data['news_url'].'calendar/archives/'.$dataCalendar->calendar_id.'-'.seo_url($dataCalendar->calendar_subject),
						'image' => $dataCalendar->image->calendar_file_name.'.'.$dataCalendar->image->calendar_file_ext,
						'name' => $dataCalendar->calendar_subject
					);					
				break;									
				default:
					$dataModuleResult = $this->solr->query($searchSolr)->row();
					$dataResult[] = array(
						'module' => $module_name,
						'url' => $this->data[''.$module_name.'_url'].(in_array($module_name,array('movie')) ? 'play/':'archives/').preg_replace('/'.$module_name.'_/','',$dataModuleResult->id).'-'.seo_url($dataModuleResult->{$module_name.'_name_s'}),
						'image' => $dataModuleResult->{$module_name.'_image_name_s'},
						'name' => $dataModuleResult->{$module_name.'_name_s'}
					);					
			endswitch;
		endforeach;
		return $dataResult;
	}
}

?>